﻿namespace ThingsGateway.Foundation
{
    /// <summary>消息类的基类</summary>
    public abstract class NetMessageBase : Result<byte[]>, INetMessage
    {

        #region Private Fields

        private byte[] sendBytes = new byte[] { };

        #endregion Private Fields

        #region Public Properties

        public int BodyLength { get; set; }
        public byte[] HeadBytes { get; set; }

        public virtual int ProtocolHeadBytesLength { get; }
        public byte[] ReceivedBytes { get; set; }

        public byte[] SendBytes
        {
            get
            {
                return sendBytes;
            }
            set
            {
                sendBytes = value;
                SendBytesThen();
            }

        }

        #endregion Public Properties

        #region Public Methods

        public abstract bool CheckHeadBytesLegal(byte[] head);

        public abstract NetMessageBase NewMessage();

        #endregion Public Methods

        #region Protected Methods
        /// <summary>
        /// 写入<see cref="SendBytes"/>后触发此方法
        /// </summary>
        protected abstract void SendBytesThen();

        #endregion Protected Methods
    }
}